package com.koushikdutta.ion;

import android.graphics.drawable.Drawable;
import android.os.Looper;
import android.view.ViewGroup.LayoutParams;
import android.view.animation.Animation;
import android.widget.ImageView;
import com.koushikdutta.ion.IonDrawable.ImageViewFutureImpl;
import com.koushikdutta.ion.bitmap.BitmapInfo;
import com.koushikdutta.ion.builder.Builders.IV.F;
import com.koushikdutta.ion.future.ImageViewFuture;

public class IonImageViewRequestBuilder extends IonBitmapRequestBuilder implements F {
    static final /* synthetic */ boolean $assertionsDisabled = (!IonImageViewRequestBuilder.class.desiredAssertionStatus());
    private static final ImageViewFutureImpl FUTURE_IMAGEVIEW_NULL_URI = new ImageViewFutureImpl() {
        {
            setComplete(new NullPointerException("uri"));
        }
    };
    Drawable errorDrawable;
    int errorResource;
    ImageViewContextReference imageViewPostRef;
    Animation inAnimation;
    int inAnimationResource;
    Animation loadAnimation;
    int loadAnimationResource;
    Drawable placeholderDrawable;
    int placeholderResource;

    public /* bridge */ /* synthetic */ String computeBitmapKey(String x0) {
        return super.computeBitmapKey(x0);
    }

    public IonImageViewRequestBuilder(Ion ion) {
        super(ion);
    }

    void reset() {
        super.reset();
        this.imageViewPostRef = null;
        this.placeholderDrawable = null;
        this.placeholderResource = 0;
        this.errorDrawable = null;
        this.errorResource = 0;
        this.inAnimation = null;
        this.inAnimationResource = 0;
        this.loadAnimation = null;
        this.loadAnimationResource = 0;
    }

    protected IonRequestBuilder ensureBuilder() {
        if (this.builder == null) {
            this.builder = new IonRequestBuilder(ContextReference.fromContext(this.imageViewPostRef.getContext().getApplicationContext()), this.ion);
        }
        return this.builder;
    }

    public ImageViewFuture load(String uri) {
        ensureBuilder();
        this.builder.load(uri);
        return intoImageView((ImageView) this.imageViewPostRef.get());
    }

    IonImageViewRequestBuilder withImageView(ImageView imageView) {
        this.imageViewPostRef = new ImageViewContextReference(imageView);
        return this;
    }

    private IonDrawable setIonDrawable(ImageView imageView, BitmapInfo info, int loadedFrom) {
        IonDrawable ret = IonDrawable.getOrCreateIonDrawable(imageView).ion(this.ion).setBitmap(info, loadedFrom).setSize(this.resizeWidth, this.resizeHeight).setError(this.errorResource, this.errorDrawable).setPlaceholder(this.placeholderResource, this.placeholderDrawable).setInAnimation(this.inAnimation, this.inAnimationResource).setDisableFadeIn(this.disableFadeIn);
        imageView.setImageDrawable(ret);
        return ret;
    }

    public ImageViewFuture intoImageView(ImageView imageView) {
        if (!$assertionsDisabled && Thread.currentThread() != Looper.getMainLooper().getThread()) {
            throw new AssertionError();
        } else if (imageView == null) {
            throw new NullPointerException("imageView");
        } else if (this.builder.uri == null) {
            setIonDrawable(imageView, null, 0).cancel();
            return FUTURE_IMAGEVIEW_NULL_URI;
        } else {
            withImageView(imageView);
            BitmapFetcher bitmapFetcher = executeCache();
            IonDrawable drawable;
            ImageViewFuture imageViewFuture;
            if (bitmapFetcher.info != null) {
                IonBitmapRequestBuilder.doAnimation(imageView, null, 0);
                drawable = setIonDrawable(imageView, bitmapFetcher.info, 0);
                drawable.cancel();
                imageViewFuture = drawable.getFuture();
                imageViewFuture.reset();
                imageViewFuture.setComplete(bitmapFetcher.info.exception, imageView);
                return imageViewFuture;
            }
            drawable = setIonDrawable(imageView, null, 0);
            IonBitmapRequestBuilder.doAnimation(imageView, this.loadAnimation, this.loadAnimationResource);
            imageViewFuture = drawable.getFuture();
            imageViewFuture.reset();
            drawable.register(this.ion, bitmapFetcher.bitmapKey);
            if (BitmapFetcher.shouldDeferImageView(this.ion)) {
                bitmapFetcher.defer();
                return imageViewFuture;
            }
            bitmapFetcher.execute();
            return imageViewFuture;
        }
    }

    protected void finalizeResize() {
        if (this.resizeWidth <= 0 || this.resizeHeight <= 0) {
            LayoutParams lp = ((ImageView) this.imageViewPostRef.get()).getLayoutParams();
            if (lp != null) {
                if (this.resizeWidth <= 0 && lp.width > 0) {
                    this.resizeWidth = lp.width;
                }
                if (this.resizeHeight <= 0 && lp.height > 0) {
                    this.resizeHeight = lp.height;
                }
            }
        }
    }

    public IonImageViewRequestBuilder error(int resourceId) {
        this.errorResource = resourceId;
        return this;
    }

    public IonImageViewRequestBuilder animateIn(int animationResource) {
        this.inAnimationResource = animationResource;
        return this;
    }
}
